using UnityEngine;
using System.Collections;

public class Switch : MonoBehaviour 
{
	public Door door;
	public bool head_switch;
	
	private bool activated = false;
	private int targetLayer;
	
	public void Start ()
	{
		targetLayer = head_switch ? 9 : 10;

		if (head_switch)
			renderer.material.SetColor ("_Color", Color.blue);
		else
			renderer.material.SetColor ("_Color", Color.red);
	}
	
	public void OnCollisionEnter (Collision other)
	{
		if (activated)
			return;
		
		SnakeBody snakeBody = other.gameObject.GetComponent <SnakeBody> ();
		
		if (snakeBody == null)
			return;
		
		if (snakeBody.WheelMode)
			return;
		
		if (other.gameObject.layer == targetLayer)
		{
			activated = true;
			StartCoroutine ("Activate");
		}
	}
	
	private IEnumerator Activate ()
	{
		door.SendMessage ("SwitchActivated");
		
		while (transform.localScale.x > 0.2)
		{
			transform.localScale = transform.localScale * 3 / 4;
			yield return null;
		}
		
		Destroy (gameObject);
	}
}